from django import template
from django.core.urlresolvers import resolve
from blog.comment.forms import CommentForm
from blog.models import Post

register = template.Library()

@register.inclusion_tag('blog/comment/_list.html', takes_context = True)
def comment_render_list(context):
    request = context['request']
    func, args, kwargs = resolve(request.path)
    post_id = kwargs['post_id']
    comments = Post.objects.get(pk = post_id).comment_set.all()
    return {'comments': comments}

@register.inclusion_tag('blog/comment/_form.html', takes_context = True)
def comment_render_form(context):
    request = context['request']
    comment_form = CommentForm()
    form  = hasattr(request, 'rebounded_comment_form') and request.rebounded_comment_form or comment_form
    return {'form': form}
